package com.vitabuybuy.tv.ui.mine.talent.adapter

import android.widget.EditText
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.vitabuybuy.tv.dto.CaseBean
import com.library.common.base.MBaseRecycAdapter

class CaseAdapter : MBaseRecycAdapter<CaseBean>() {
    override fun normalViewId(): Int {
        return R.layout.item_case
    }

    override fun onHolder(holder: ViewHolder, position: Int) {
        holder.getView<ImageView>(R.id.ivImage)?.apply {
            if (mData[position]?.image.isNullOrEmpty()) {
                setImageResource(R.drawable.add_image)
            } else {
                ImageAdapter.setNomalImageUrl(
                    this, mData[position]?.image,
                    ContextCompat.getDrawable(mContext!!, R.drawable.default_image)
                )
            }
            setOnClickListener {
                onItemImageListenter(position)
            }
        }
        holder.getView<ImageView>(R.id.iv_delete_case)?.setOnClickListener {
            removeAt(position)
        }
        holder.getView<EditText>(R.id.etEdit)?.apply {
            if (!mData[position]?.content.isNullOrEmpty()) {
                setText(mData[position]?.content)
            }
            addTextChangedListener {
                mData[position]?.content = it.toString()
            }
        }
    }

    lateinit var onItemImageListenter: (position: Int) -> Unit
}
